<?php

/**
 * This is the model class for table "contact_person".
 *
 * The followings are the available columns in table 'contact_person':
 * @property integer $id_contact
 * @property integer $id_client
 * @property string $first_name
 * @property string $last_name
 * @property string $street
 * @property string $nr_street
 * @property string $zip_code
 * @property string $city
 * @property string $email
 * @property string $mobile
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class ContactPerson extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return ContactPerson the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'contact_person';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_client, first_name, last_name, email, zip_code', 'required'),
            array('email','email'),
            array('email','unique', 'message'=>Yii::t('app', 'This email already exists.')),
			array('id_client, del_flg, mobile', 'numerical', 'integerOnly'=>true),
			array('first_name, last_name, street, nr_street, email', 'length', 'max'=>128),
			array('zip_code', 'length', 'max'=>12),
			array('city', 'length', 'max'=>64),
			array('mobile, phone', 'length', 'max'=>20),
			array('date_add, date_upd', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_contact, id_client, first_name, last_name, street, nr_street, zip_code, city, email, mobile, date_add, date_upd, del_flg, phone', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'cli' => array(self::BELONGS_TO, 'Client', 'id_client')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_contact' => '#',
			'id_client' => Yii::t('app', 'Id Client'),
			'first_name' => Yii::t('app', 'First Name'),
			'last_name' => Yii::t('app', 'Last Name'),
			'street' => Yii::t('app', 'Street'),
			'nr_street' => Yii::t('app', 'Nr Street'),
			'zip_code' => Yii::t('app', 'Zip Code'),
			'city' => Yii::t('app', 'City'),
			'email' => Yii::t('app', 'Email'),
			'mobile' => Yii::t('app', 'Mobile'),
			'phone' => Yii::t('app', 'Phone'),
			'date_add' => Yii::t('app', 'Date Add'),
			'date_upd' => Yii::t('app', 'Date Update'),
			'del_flg' => 'Del Flg',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_contact',$this->id_contact);
		$criteria->compare('id_client',$this->id_client);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('street',$this->street,true);
		$criteria->compare('nr_street',$this->nr_street,true);
		$criteria->compare('zip_code',$this->zip_code,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('date_add',$this->date_add,true);
		$criteria->compare('date_upd',$this->date_upd,true);
		$criteria->compare('del_flg',$this->del_flg);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }

    public static function _getContactByClient($id){
        $criteria=new CDbCriteria;
        $criteria->addCondition('id_client = :id_client');
        $criteria->addCondition('del_flg = 0');
        $criteria->params = array(':id_client' => (int)$id);
        $mContact = ContactPerson::model()->findAll($criteria);
        if($mContact){
            return $mContact;
        }else{
            return false;
        }
    }

    public static function getObjContact(){
        $criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        $dataObj = self::model()->findAll($criteria);
        if($dataObj){
            return $dataObj;
        }else{
            return false;
        }
    }

    public static function getListContact($client = false, $id_client = 0){
        if($client===true)
            $dataObj = self::_getContactByClient($id_client);
        else
            $dataObj = self::getObjContact();

        $result = array();
        if($dataObj){
            foreach($dataObj as $data){
                $result[$data->id_contact] = $data->first_name.' '.$data->last_name;
            }
        }
        return $result;
    }

    public static function getObjContactDetail($id){
        $criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        return self::model()->findByPk($id, $criteria);
    }
}